﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DTO;

namespace QuanLyNhaSach.MainMenu
{
    public partial class FrmPayMoney : DevComponents.DotNetBar.Office2007Form
    {
        PayMoneyBUS paymoneyBus;

        public ActionName Action { get; set; }
        public PayMoneyDTO PayMoneyDto { get; set; }
        public FrmPayMoney()
        {
            InitializeComponent();
            paymoneyBus = new PayMoneyBUS();
            dtpCustomerBirthday.Format = DevComponents.Editors.eDateTimePickerFormat.Custom;
            dtpPayMoney.Format = DevComponents.Editors.eDateTimePickerFormat.Custom;
            dtpCustomerBirthday.CustomFormat = Global.DateTimeFormat;
            dtpPayMoney.CustomFormat = Global.DateTimeFormat;
            dtpPayMoney.Value = DateTime.Now;
            btnUpdate.Location = btnAdd.Location;
            DocSequenceBUS docsequenceBus = new DocSequenceBUS();
            txtPayMoneyCode.Text = docsequenceBus.GetNextDocSequenceNumber(DocSequence.Payment.ToString());
            PayMoneyDto = new PayMoneyDTO();
        }

        private void btnFillCustomer_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtCustomerCode.Text))
            {
                MessageBoxEx.Show("Chưa nhập Mã khách hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            CustomerBUS customerBUS = new CustomerBUS();
            CustomerDTO customerDTO = customerBUS.GetCustomerByCode(txtCustomerCode.Text);
            if (customerDTO != null)
            {
                txtCustomerID.Text = customerDTO.CustomerID.ToString();
                txtCustomerAddress.Text = customerDTO.CustomerAddress;
                txtCustomerCMND.Text = customerDTO.CustomerCMND;
                txtCustomerEmail.Text = customerDTO.CustomerEmail;
                txtCustomerName.Text = customerDTO.CustomerName;
                dbiDebtMoney.Value = (int)customerDTO.CustomerDebtMoney;
                dtpCustomerBirthday.Value = customerDTO.CustomerBirthday;
                rdbMale.Checked = customerDTO.CustomerSex;
                rdbFeMale.Checked = !rdbMale.Checked;
            }
            else
            {
                txtCustomerID.Text = string.Empty;
                txtCustomerAddress.Text = string.Empty;
                txtCustomerCMND.Text = string.Empty;
                txtCustomerEmail.Text = string.Empty;
                txtCustomerName.Text = string.Empty;
                dbiDebtMoney.Value = 0;
                rdbMale.Checked = true;
                rdbFeMale.Checked = false;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
            {
                return;
            }

            PayMoneyDto.PayMoneyCode = txtPayMoneyCode.Text;
            PayMoneyDto.PayMoneyCustomerID = int.Parse(txtCustomerID.Text);
            PayMoneyDto.Money = dbiPayMoney.Value;
            PayMoneyDto.PayMoneyCreateDate = (DateTime)dtpPayMoney.Value;
            PayMoneyDto.PayMoneyUserID = Global.UserID;
            PayMoneyDto.Status = true;
            PayMoneyDto.PayMoneyNote = txtPayMoneyNote.Text;

            PayMoneyDto.PayMoneyID = paymoneyBus.InsertPayMoney(PayMoneyDto);
            if (PayMoneyDto.PayMoneyID > 0)
            {
                
                //cap nhat tien no
                CustomerBUS cusBus = new CustomerBUS();
                double debtMoney = dbiDebtMoney.Value - dbiPayMoney.Value;
                int cusID = int.Parse(txtCustomerID.Text);
                if (!cusBus.UpdateCustomerDebtMoney(cusID, debtMoney))
                {
                    Global.SetMessage(lblMessage, "Cập nhật tiền nợ chưa thành công!", false);
                    return;
                }
                 
                Global.SetMessage(lblMessage, "Thu tiền thành công!", true);
                Action = ActionName.Update;
                ProcessButton();
                btnFillCustomer_Click(sender, e);
            }
            else
            {
                Global.SetMessage(lblMessage, "Thu tiền không thành công!", false);
            }
        }

        private void ProcessButton()
        {
            if (Action == ActionName.Insert)
            {
                btnUpdate.Visible = false;
                btnAdd.Visible = true;
                txtCustomerCode.ReadOnly = false;
            }
            else if (Action == ActionName.Update)
            {
                btnUpdate.Visible = true;
                btnAdd.Visible = false;
                txtCustomerCode.ReadOnly = true;
            }
        }

        private bool CheckDataInput()
        {
                       
            if (string.IsNullOrEmpty(txtCustomerID.Text))
            {
                Global.SetMessage(lblMessage, "Chưa tìm kiếm khách hàng trả tiền!", false);
                txtCustomerCode.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtPayMoneyCode.Text) || dbiPayMoney.Value == 0)
            {
                Global.SetMessage(lblMessage, "Tiền thu chưa nhập!", false);
                dbiPayMoney.Focus();
                return false;
            }

            if (dbiDebtMoney.Value < dbiPayMoney.Value)
            {
                Global.SetMessage(lblMessage, "Số tiền thu không vượt quá số tiền nợ.", false);
                dtpPayMoney.Focus();
                return false;
            }
            

            
            return true;
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            dtpPayMoney.Value = DateTime.Now;
            DocSequenceBUS docsequenceBus = new DocSequenceBUS();
            txtPayMoneyCode.Text = docsequenceBus.GetNextDocSequenceNumber(DocSequence.Payment.ToString());
            txtPayMoneyNote.Text = string.Empty;
            dbiPayMoney.Value = 0;
            txtCustomerCode.ReadOnly = false;
            txtCustomerID.Text = string.Empty;
            txtCustomerCode.Text = string.Empty;
            txtCustomerAddress.Text = string.Empty;
            txtCustomerCMND.Text = string.Empty;
            txtCustomerEmail.Text = string.Empty;
            txtCustomerName.Text = string.Empty;
            dbiPayMoney.Focus();
            Action = ActionName.Insert;
            ProcessButton();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!CheckDataInput())
                return;

            PayMoneyDto.PayMoneyNote = txtPayMoneyNote.Text;
            PayMoneyDto.Money = dbiPayMoney.Value;

            if (paymoneyBus.UpdatePayMoney(PayMoneyDto))
                Global.SetMessage(lblMessage, "Cập nhật thành công!", true);
            else
            {
                Global.SetMessage(lblMessage, "Cập nhật không thành công!", false);
                return;
            }

            //cap nhat tien no
            CustomerBUS cusBus = new CustomerBUS();
            double debtMoney = dbiDebtMoney.Value - dbiPayMoney.Value;
            int cusID = int.Parse(txtCustomerID.Text);
            if (!cusBus.UpdateCustomerDebtMoney(cusID, debtMoney))
            {
                Global.SetMessage(lblMessage, "Cập nhật tiền nợ thành công!", false);
                return;
            }
            btnFillCustomer_Click(sender, e);
        }
    }
}
